package top.infopub.mgr.common.vo;


import java.util.HashMap;
import java.util.Map;


/**
 * 请严格准命名规范
 * ERROR_系统代号_错误类型 <br />
 * @author Awoke
 * @version 2018年9月18日
 * @see ErrorCodeEnum
 * @since
 */
public enum ErrorCodeEnum {

    //---1x---//
    /**
     * 1001,"参数不符合规则"
     */
    ERROR_COMMON_PARAM(1001, "参数不符合规则~"),
    /**
     * 1002,"系统数据异常~" 
     */
    ERROR_COMMON_SYSTEM(1002, "系统数据异常~"),
    /**
     * 1003,"系统处理中"
     */
    ERROR_COMMON_HANDING(1003, "系统处理中~"),
    /**
     * 1004,"保存数据失败"
     */
    ERROR_COMMON_SAVE(1004, "保存数据失败~"),
    /**
     * 1005,"违反了唯一约束"
     */
    ERROR_COMMON_UNIQUE(1005, "违反唯一约束~"),
    /**
     * 1006,"条件验证异常"
     */
    ERROR_COMMON_CHECK(1006, "条件验证异常~"),
    /**
     * 1007,"重复处理"
     */
    ERROR_COMMON_REPEAT_HANDLER(1007, "重复处理~"),
    /**
     * 1008,"网络超时"
     */
    ERROR_COMMON_NETWORK_ERROR(1008, "网络超时~"),
    /**
     * 1009,"编号已经存在"
     */
    ERROR_COMMON_CODE_UK(1009, "编号已经存在~");

    private int code;

    private String returnMsg;

    ErrorCodeEnum(int code, String returnMsg) {
        this.code = code;
        this.returnMsg = returnMsg;
    }

    /**
     * Enum对应的RspVo，isSuccess=false
     * @return 
     * @see
     */
    @SuppressWarnings("rawtypes")
    public RspVo getRspVo() {
        return new RspVo(false, this.code, this.returnMsg);
    }

    /**
     * Enum对应的RspVo，msg可以自定义，isSuccess=false
     * @return 
     * @see
     */
    @SuppressWarnings("rawtypes")
    public RspVo getRspVo(String msg) {
        return new RspVo(false, this.code, msg);
    }

    public static String getMsgByCode(int code) {
        synchronized (lock) {
            if (params == null) {
                params = new HashMap<Integer, ErrorCodeEnum>();
                for (ErrorCodeEnum e : ErrorCodeEnum.values()) {
                    params.put(e.code, e);
                }
            }
        }
        return params.get(code).returnMsg;
    }

    public static ErrorCodeEnum getEnumByCode(int code) {
        synchronized (lock) {
            if (params == null) {
                params = new HashMap<Integer, ErrorCodeEnum>();
                for (ErrorCodeEnum e : ErrorCodeEnum.values()) {
                    params.put(e.code, e);
                }
            }
        }
        return params.get(code);
    }

    private static Map<Integer, ErrorCodeEnum> params;

    private static Object lock = new Object();

    public int getCode() {
        return this.code;
    }

}
